﻿using System;
using UIKit;
using Moven.Controls;
using Moven.iOS.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using MaterialControls;
using System.Drawing;
using System.ComponentModel;

[assembly: ExportRenderer(typeof(MaterialEntry), typeof(MaterialEntryRenderer))]
namespace Moven.iOS.Renderers
{
	public class MaterialEntryRenderer : ViewRenderer<MaterialEntry, MDTextField>
	{
		//
		// Fields
		//
		UIColor _defaultTextColor;

		static readonly UIColor SeventyPercentGrey = new UIColor(0.7f, 0.7f, 0.7f, 1);

		//
		// Properties
		//
		IElementController ElementController
		{
			get
			{
				return Element;
			}
		}

		//
		// Constructors
		//
		public MaterialEntryRenderer()
		{
			Frame = new RectangleF(0f, 20f, 320f, 40f);
		}

		//
		// Methods
		//
		protected override void Dispose(bool disposing)
		{
			if (disposing && Control != null)
			{
				Control.EditingChanged -= OnEditingChanged;
			}
			base.Dispose(disposing);
		}

		void OnEditingChanged(object sender, EventArgs eventArgs)
		{
			this.ElementController.SetValueFromRenderer(Entry.TextProperty, Control.Text);
		}

		protected override void OnElementChanged(ElementChangedEventArgs<MaterialEntry> e)
		{
			base.OnElementChanged(e);
			MDTextField uITextField = Control;
			if (uITextField == null)
			{
				SetNativeControl(uITextField = new MDTextField());
				_defaultTextColor = uITextField.TextColor;
				uITextField.EditingChanged += OnEditingChanged;
				uITextField.ShouldReturn = OnShouldReturn;
			}
			if (e.NewElement != null)
			{
				UpdatePlaceholder();
				UpdatePassword();
				UpdateText();
				UpdateColor();
				UpdateKeyboard();
			}
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == Entry.PlaceholderProperty.PropertyName || e.PropertyName == Entry.PlaceholderColorProperty.PropertyName)
			{
				this.UpdatePlaceholder();
			}
			else if (e.PropertyName == Entry.IsPasswordProperty.PropertyName)
			{
				this.UpdatePassword();
			}
			else if (e.PropertyName == Entry.TextProperty.PropertyName)
			{
				this.UpdateText();
			}
			else if (e.PropertyName == Entry.TextColorProperty.PropertyName)
			{
				this.UpdateColor();
			}
			else if (e.PropertyName == Xamarin.Forms.InputView.KeyboardProperty.PropertyName)
			{
				UpdateKeyboard();
			}
			else if (e.PropertyName == VisualElement.IsEnabledProperty.PropertyName)
			{
				this.UpdateColor();
				this.UpdatePlaceholder();
			}
			base.OnElementPropertyChanged(sender, e);
		}

		void UpdateKeyboard()
		{
			var keyboard = Element.Keyboard;
			if (keyboard == Keyboard.Default)
			{
				Control.KeyboardType = UIKeyboardType.Default;
			}
			else if (keyboard == Keyboard.Numeric)
			{
				Control.KeyboardType = UIKeyboardType.NumberPad;
			}
			else if (keyboard == Keyboard.Telephone)
			{
				Control.KeyboardType = UIKeyboardType.PhonePad;
			}
			else if (keyboard == Keyboard.Email)
			{
				Control.KeyboardType = UIKeyboardType.EmailAddress;
			}
			else if (keyboard == Keyboard.Url)
			{
				Control.KeyboardType = UIKeyboardType.Url;
			}
		}

		bool OnShouldReturn(MDTextField view)
		{
			Control.ResignFirstResponder();
			((IEntryController)Element).SendCompleted();
			return true;
		}

		void UpdateColor()
		{
			Color textColor = Element.TextColor;
			if (textColor.IsDefault() || !Element.IsEnabled)
			{
				Control.TextColor = this._defaultTextColor;
				return;
			}
			Control.TextColor = textColor.ToUIColor();
		}

		void UpdatePassword()
		{
			if (Element.IsPassword && Control.IsFirstResponder)
			{
				Control.Enabled = false;
				Control.SecureTextEntry = true;
				Control.Enabled = Element.IsEnabled;
				Control.BecomeFirstResponder();
				return;
			}
			Control.SecureTextEntry = Element.IsPassword;
		}

		void UpdatePlaceholder()
		{
			var placeholder = Element.Placeholder;
			if (placeholder == null)
			{
				return;
			}
			Control.Label = placeholder;
		}

		void UpdateText()
		{
			if (Element != null && Control.Text != Element.Text && !Element.Text.IsNullOrEmpty())
			{
				Control.Text = Element.Text;
			}
		}
	}
}
